#! /usr/bin/env python
# -*- coding: utf-8 -*-

from django.shortcuts import render, HttpResponse, redirect
from web.form.project import ProjectModelForm
from django.http import JsonResponse
from web import models
from utils.tencent.cos import create_bucket
import time
from django.conf import settings


def project_list(request):
    """ 项目列表 """
    if request.method == 'GET':
        form = ProjectModelForm(request)
        project_dict = {'star': [], 'my': [], 'join': []}
        my_project_list = models.Project.objects.filter(creator=request.tracer.user)
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({"value": row, "type": 'my'})
            else:
                project_dict['my'].append(row)
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)
        for row in join_project_list:
            if row.star:
                project_dict['star'].append({"value": row.project, "type": 'join'})
            else:
                project_dict['join'].append(row.project)

        return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict})

    if request.method == 'POST':
        form = ProjectModelForm(request, data=request.POST)

        if form.is_valid():
            # 为项目创建桶
            bucket = f'{request.tracer.user.mobile_phone}-{int(time.time()*1000)}-{settings.APP_ID}'
            create_bucket(bucket, settings.REGION)
            # 把桶和区域写入
            form.instance.bucket = bucket
            form.instance.region = settings.REGION
            # 验证通过：项目名、颜色、描述 + 创建者
            form.instance.creator = request.tracer.user

            instance = form.save()
            # 初始化问题类型
            issues_type_obj_list = []
            for item in models.IssuesType.PROJECT_INIT_LIST:
                issues_type_obj_list.append(models.IssuesType(project=instance, title=item))
            # 批量添加
            models.IssuesType.objects.bulk_create(issues_type_obj_list)
            return JsonResponse({'status': True})

        return JsonResponse({'status': False, 'err': form.errors})


def project_star(request, project_type, project_id):
    """ 星标项目"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')

    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    """ 星标项目"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')

    return HttpResponse('请求错误')
